package fr.umlv.escapeir.painters;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;

import fr.umlv.escapeir.GameWorld;

public class ExplosionPainter extends AnimatedWorldPainter {
	
	private final Image boom;
	private final Body body;
	
	private float size;
	private final float padding;
	
	private int tick;
	private final int time;
	
	public ExplosionPainter(Body body, int time,  int size) {
		Image boomTmp;
		try {
			boomTmp = ImageLoader.getExplosionImage();
		} catch (IOException e) {
			boomTmp = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
			Graphics2D painter = (Graphics2D) boomTmp.getGraphics();
			painter.drawString("Image can't be loaded", 0, 0);
		}
		boom = boomTmp;
		this.time = time*GameWorld.STEP_NUMBER;
		this.body = body;
		this.padding = (float)size / this.time;
	}
	
	@Override
	public void render(WorldGraphics2D painter) {
		if (tick > time) return;
		painter.save();
		painter.translateFromWorld(body.getPosition());
		painter.rotate(body.getAngle());
		painter.drawRectWithImage(new Vec2(-size, size), new Vec2(size, -size), boom);
		painter.restore();
	}

	@Override
	public void apply() {
		if (tick > time) return;
		++tick;
		size += padding;
	}

}
